package com.why.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.blog.common.BusinessRuntimeException;
import com.why.blog.common.JwtTokenUtils;
import com.why.blog.common.R;
import com.why.blog.common.ResponseCode;
import com.why.blog.common.utils.TimestampUtil;
import com.why.blog.dao.mapper.HaloCommentMapper;
import com.why.blog.entity.HaloComment;
import com.why.blog.entity.HaloPost;
import com.why.blog.entity.VO.CommentVO;
import com.why.blog.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TriggerWu
 * @since 2019-04-28
 */
@Service
public class HaloCommentServiceImpl implements CommentService {

    @Autowired
    private HaloCommentMapper commentMapper;

    @Autowired
    Mapper beanMapper;

    @Override
    public Integer saveComment(CommentVO co, String tokenHeader) {
        if (null == co) {
            return -1;
        }
        if (StringUtils.isNotBlank(tokenHeader)) {
            String token = tokenHeader.replace(JwtTokenUtils.TOKEN_PREFIX, "");
            String userId = JwtTokenUtils.getUserId(token);
            //判断当前发送人是否是发帖人 获取token判定
            if (StringUtils.isNotBlank(userId) && co.getCommentParent().equals(userId)) {
                co.setIsAdmin(1);
            }
        } else {
            co.setIsAdmin(0);
        }
        HaloComment comment = beanMapper.map(co, HaloComment.class);
        comment.setCommentDate(TimestampUtil.currentTimeStamp());
        return commentMapper.insert(comment);
    }

    @Override
    public R removeComment(String coId) {
        int res = commentMapper.deleteById(coId);
        if (res > 0) {
            return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), ResponseCode.RESPONSE_SUCCESS.getMsg());
        }
        return R.getInstance(ResponseCode.ARG_ERROR.getCode(), ResponseCode.ARG_ERROR.getMsg());
    }

    @Override
    public IPage<HaloComment> showList(CommentVO co, Integer page, Integer pageSize) {
        IPage<HaloComment> ipg = new Page<>(page, pageSize);
        QueryWrapper<HaloComment> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(co.getPostId())) {
            wrapper.eq("post_id", co.getPostId());
        }
        wrapper.orderByDesc("comment_date");
        ipg = commentMapper.selectPage(ipg, wrapper);
        return ipg;
    }

    @Override
    public R showParent(String parentId) {
        List<HaloComment> clist = new ArrayList<>();
        if (StringUtils.isBlank(parentId)) {
            return R.getInstance(ResponseCode.ARG_ERROR.getCode(), "该评论已删除");
        }
        String[] parr = parentId.split(",");
        for (int i = 0; i < parr.length; i++) {
            if (StringUtils.isNotBlank(parr[i])) {
                HaloComment comment = commentMapper.selectById(parr[i]);
                if (comment != null) {
                    clist.add(comment);
                }

            }
        }
        if (clist.size() == 0 || clist == null) {
            return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), "该评论已删除");
        }
        return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), ResponseCode.RESPONSE_SUCCESS.getMsg(), clist);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R frontComment(CommentVO co) {
        if (null == co) {
            return R.getInstance(ResponseCode.ARG_ERROR.getCode(), "不存在此回复人！");
        }
        Integer res = 0;
        try {
            //回复他人
            if (null != co.getCommentParent()) {
                res = commentMapper.addCommentChild(co.getCommentChild(), co.getCommentId());
            }

            HaloComment com = beanMapper.map(co, HaloComment.class);
            com.setCommentDate(TimestampUtil.currentTimeStamp());
            int cres = commentMapper.insert(com);
            if (cres > 0) {
                return R.getInstance(ResponseCode.RESPONSE_SUCCESS.getCode(), ResponseCode.RESPONSE_SUCCESS.getMsg());
            }


        } catch (Exception e) {
            throw new BusinessRuntimeException(ResponseCode.SYSTEM_ERROR, "发送失败,请稍后再试!");
        }
        return R.getInstance(ResponseCode.ARG_ERROR.getCode(), ResponseCode.ARG_ERROR.getMsg());
    }

    @Override
    public Integer commentsCount(String postId) {
        return commentMapper.selectCount(new QueryWrapper<HaloComment>().eq("post_id",postId));
    }
}
